<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>短信项目模板配置管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script>
        layui.use(['table', 'form', 'jquery', 'element','layer'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
           var layer=layui.layer;
           var user;
            var options = {
                elem: "#table",
                id: "modelReload",
                url: '/template2/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                loading: true,
                page: {
                    count: "count",
                    limit: 10,
                    limits: [10, 20, 50],
                    layout: ['prev', 'page', 'next', 'skip', 'limit', 'count'],
                    prev: "上一页",
                    next: "下一页",
                    groups: 5
                },
                cols: [[ //表头
                    {field: 'belong', title: '地市/区县'},
                    {field: 'name', title: '项目名称'},
                    {field: 'cityExtId', title: '短信通道号', templet:function(d){
                    	if(d.cityId=='11'){
                    		return "10657"+"1101"+d.cityExtId;
                    	}else{
                    		return "10657"+"1102"+d.cityExtId;
                    	}
                    	
                    }},
                    {field: 'stateName', title: '项目状态', templet:function(d){
                    	if(d.state=='0'){
                    		return '<div style="color:green;">'+d.stateName+'</div>'
                    	}else if(d.state=='1'||d.state=='3'){
                    		return '<div style="color:blue;">'+d.stateName+'</div>'
                    	}else if(d.state=='2'){
                    		return '<div style="color:red;">'+d.stateName+'</div>'
                    	}
                    	
                    }},
                    {field: 'templateStateName', title: '审核状态', templet:function(d){
                    	if(d.templateState=='2'){
                    		return '<div style="color:green;">'+d.templateStateName+'</div>'
                    	}else if(d.templateState=='3'){
                    		return '<div style="color:red;">'+d.templateStateName+'</div>'
                    	}else{
                    		return '<div>'+d.templateStateName+'</div>'
                    	}
                    	
                    }},
                    {field: 'stationNum', title: '基站数量'},
                    {field: 'operation', title: '操作', toolbar: '#barDemo'}
                ]],
                done: function (res) {
                    // console.log(JSON.stringify(res));
                }
            };
            table.render(options);
            table.on('tool(tableElement)', function (obj) {
                var data = obj.data;
                if(data.templateState !=null) {
                    if (obj.event == 'detail') {
                        window.location.href = 'smsTemplateView2.html?mgrId=' + data.id;
                    } else if (obj.event == 'modify') {
                        window.location.href = 'smsTemplateModify2.html?mgrId=' + data.id +"&cityId=" + data.cityId;
                    } else if (obj.event == 'config') {
                        layer.msg("该项目已经有一个模板了，无法配置新的模板，请点击修改来修改模板的属性");
                    } else if (obj.event == 'check') {
                        window.location.href = 'smsTemplateCheck2.html?mgrId=' + data.id +"&stationNum=" + data.stationNum;
                    } else if (obj.event == data.state) {
                        layer.msg("该项目已经是该状态了");
                    } else {
                        layer.confirm('您确定要修改该状态吗？', {icon: 3, title: '提示'}, function (index) {
                         if(obj.event==0){
                                	 	 var url2 = '/citySign/find';
	                                	 var param2={
	                                	 	cityId:data.cityId,
	                                	 	page:1,
	                                	 	limit:1
	                                	 };
	                                	 $.ajax({
			                                type: 'post',
			                                data: param2,
			                                url: url2,
			                                success:function(obj2){
			                                	if(obj2.data[0].signId==2){
			                                		layer.msg('该地市已欠费，无法开启!');
			                                	}else{
			                                		var url = "/busi/modify";
						                            var param = {
						                                id: data.id,
						                                state: obj.event
						                            };
						                            $.ajax({
						                                type: 'post',
						                                data: param,
						                                url: url,
						                                success: function (json) {
						                                    if (json.code == 0) {
						                                        layer.confirm('修改成功', {
						                                            btn: ['确认'] //按钮
						                                        }, function () {
						                                            location.href = "smsTemplateManage2.html";
						                                        });
						                                    }
						                                    else {
						                                       layer.msg(json.data.msg);
						                                    }
						                                },error:function(){
						                                	layer.msg('系统异常，请联系管理员处理!');
						                                },
						                                complete: function () {
						                                }
						                            });
			                                	}
			                                },error:function(){
			                                	layer.msg('系统异常，请联系管理员处理!');
			                                }
			                             });
                        }else{
                        	var url = "/busi/modify";
						                            var param = {
						                                id: data.id,
						                                state: obj.event
						                            };
						                            $.ajax({
						                                type: 'post',
						                                data: param,
						                                url: url,
						                                success: function (json) {
						                                    if (json.code == 0) {
						                                        layer.confirm('修改成功', {
						                                            btn: ['确认'] //按钮
						                                        }, function () {
						                                            location.href = "smsTemplateManage2.html";
						                                        });
						                                    }
						                                    else {
						                                       layer.msg(json.data.msg);
						                                    }
						                                },error: function (){
						                                	layer.msg('系统异常，请联系管理员处理!');
						                                },
						                                complete: function () {
						                                }
						                            });
                        }
                        
                        
                        
                            /* var url = "/busi/modify";
                            var param = {
                                id: data.id,
                                state: obj.event
                            };
                            $.ajax({
                                type: 'post',
                                data: param,
                                url: url,
                                beforeSend: function () {
                                	alert(obj.event);
                                },
                                success: function (json) {
                                    if (json.code == 0) {
                                        layer.confirm('修改成功', {
                                            btn: ['确认'] //按钮
                                        }, function () {
                                            location.href = "smsTemplateManage2.html";
                                        });
                                    }
                                    else {
                                       layer.msg(json.data.msg);
                                    }
                                },
                                complete: function () {
                                }
                            }); */
                        });
                    }
                }else {
                    if (obj.event == 'detail') {
                        layer.msg("该项目还没有模板，请配置");
                    } else if (obj.event == 'modify') {
                        layer.msg("该项目还没有模板，请配置");
                    } else if (obj.event == 'config') {
                        window.location.href = 'smsTemplateConfigure.html?mgrId=' + data.id;
                    }
                }
            });
			//根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //监听项目  区县
            form.on('select(cityId)', function (data) {
            	var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data = selectMgrInfo(data.value);
                appendMgr(data);
            });
            //监听项目
            form.on('select(countyId)', function (data) {
            	var data1;
            	if(null!=data.value && ""!=data.value){
            		data1 = selectMgrInfo(data.value);
            	}else{
            		var cityId = $("#cityId").val();
            		data1 = selectMgrInfo(cityId);
            	}
                appendMgr(data1);
            });
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该地市下没有项目</option>');
                }
                form.render();
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            //更新数据表
            $('#query').on('click', function () {
                var mgrId = $("#mgrId").val();
                var cityId = $("#cityId").val();
                var countyId = $("#countyId").val();
                var mgrName = $("#mgrName").val();
                var mgrState = $("#mgrState").val();
                var ckeckState = $("#ckeckState").val();
                if (
                    mgrId == "" && user.duty == 2
                ) {
                    layer.msg("您必须选择一个项目");
                } else {
                    var index = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                table.reload('modelReload', {
                    method: 'post',
                    page: {
                        curr: 1
                    },
                    where: {
                        cityId: cityId,
                        countyId: countyId,
                        mgrId: mgrId,
                        mgrName:mgrName,
                        mgrState:mgrState,
                        ckeckState:ckeckState
                    }
                });
                layer.close(index);
                }
            });
            $('#add').on('click', function () {
            	location.href="smsTemplateConfigure2.html";
            });
            //身份识别
            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        if (json.duty == 1) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            //$("#shortname").attr("style", "display:none");
                            var data = selectMgrInfo(json.cityId);
                            appendMgr(data);
                            form.render();
                        } else if (json.duty == 2) {
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            //$("#shortname").attr("style", "display:none");
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            form.render();
                        }else if (json.duty == 3) {
                        	$("#add").css("visibility","hidden");
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data2 = selectCountyId(json.cityId);
                			appendCounty(data2); 
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            //$("#shortname").attr("style", "display:none");
                            var data = selectMgrInfo(json.countyId);
                            appendMgr(data);
                            form.render();
                        }
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });
        });

    </script>
    <script type="text/html" id="barDemo">
		<a href="#" lay-event="detail" id="detail">查看</a>
        <a href="#" lay-event="modify" id="modify">修改</a>
        {{# if (d.state =="0"){}}
          <a href="#" lay-event="2">关闭</a>
        {{#}}}
		{{# if (d.state =="1" || d.state =="3"){}}
		  <a href="#" lay-event="0">开启</a>
          <a href="#" lay-event="2">关闭</a>
        {{#}}}
		{{# if (d.state =="2"){}}
		  <a href="#" lay-event="0">开启</a>
        {{#}}}
		{{# if (d.templateState =="0"){}}
		  <a href="#" lay-event="check">审核</a>
        {{#}}}
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
            <span class="layui-breadcrumb" lay-separator="|">
                     <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#">短信模板管理</a>
                  <a href="#">短信项目模板配置管理2</a>
            </span>
        </div>
        <div class="content-box">
            <div class="form-select-box">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <!-- <div class="layui-inline">
                            <label class="layui-form-label">省份</label>
                            <div class="layui-input-block" style="width:110px">
                                <input type="text" name="province" lay-verify="required" value="江苏"
                                       autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div> -->
                        <div class="layui-inline">
                            <label class="layui-form-label">地市</label>
                            <div class="layui-input-block" style="width:110px">
                                <select name="cityId" lay-filter="cityId" id="cityId">
                                    <option value="">请选择</option>
                                    <option value="11">苏州</option>
                                    <option value="12">淮安</option>
                                    <option value="13">宿迁</option>
                                    <option value="14">南京</option>
                                    <option value="15">连云港</option>
                                    <option value="16">徐州</option>
                                    <option value="17">常州</option>
                                    <option value="18">镇江</option>
                                    <option value="19">无锡</option>
                                    <option value="20">南通</option>
                                    <option value="21">泰州</option>
                                    <option value="22">盐城</option>
                                    <option value="23">扬州</option>
                                    <option value="25">省级</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline countyId">
		                            <label class="layui-form-label">区县</label>
		                            <div class="layui-input-block" style="width:110px">
		                                <select name="countyId" lay-filter="countyId" id="countyId">
		                                    <option value="">全部</option>
		                                </select>
		                            </div>
		                </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-block" style="width:150px">
                                <select name="interest" lay-filter="mgrId" id="mgrId">
                                    <option value="">请先选择地市</option>
                                </select>
                            </div>
                        </div>
                        <div id ="shortname" class="layui-inline" >
                            <label class="layui-form-label">项目简称</label>
                            <div class="layui-input-block" style="width:150px">
                                  <input type="text" name="mgrName" lay-verify="required" id ="mgrName" placeholder="请输入"
                                       autocomplete="off" class="layui-input"  >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目状态</label>
                            <div class="layui-input-block" style="width:110px">
                                <select name="mgrState" lay-filter="mgrState" id="mgrState">
                                    <option value="">全部</option>
                                    <option value="0">开启</option>
                                    <option value="1">暂停</option>
                                    <option value="2">关闭</option>
                                    <option value="3">配置中</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-block" style="width:110px">
                                <select name="ckeckState" lay-filter="ckeckState" id="ckeckState">
                                    <option value="">全部</option>
                                    <option value="0">待审核</option>
                                    <option value="1">审核中</option>
                                    <option value="2">审核通过</option>
                                    <option value="3">审核不通过</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline" pane="">
                            <button class="layui-btn layui-btn-normal" id="query">查&nbsp;询</button>
                        </div>
                        <div class="layui-inline" pane="">
                            <button class="layui-btn layui-btn-normal" id="add">添&nbsp;加</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-table">
                <table id="table" lay-filter="tableElement"></table>
            </div>
        </div>
    </div>
</div>
</body>
</html>